layui.use(['table', 'admin', 'ax', 'form', 'layer', 'jquery','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;

    var txlid = Feng.getUrlParam('id');


    //YYJL
    var languageAjax = new $ax(Feng.ctxPath + "/btyTxl/yyjlList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        // $("#yyjl").append("<option value='"+val.yytel+"'>"+val.yyjl+"</option>");
        $("#yyjl").append("<option value='"+val.yyjl+"'>"+val.yyjl+"</option>");
    } );
    form.render('select');

    //天财店名
    var languageAjax = new $ax(Feng.ctxPath + "/btyTxl/tcList");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#tiancai").append("<option value='"+val.dm+"'>"+val.dm+"</option>");
    } );
    form.render('select');


    if(txlid!='' && txlid!=null ) {
        var ajax = new $ax(Feng.ctxPath + "/btyTxl/detail?id=" + txlid);
        var result = ajax.start();
        $("#HandoverCompany").val(result.data.tiancai);
        form.val('btyWmTxlForm', result.data);
    }

    /**
     * 关店按钮点击事件
     */
    var pageindex = parent.layer.getFrameIndex(window.name);
    $('#closeDialog').click(function () {
        parent.layer.close(pageindex);
    });
    /**
     * 表单提交事件
     */
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/btyTxl/waimaiAddList", function (data) {
            if(data.success){
                Feng.success("添加成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error(data.message);
            }
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message);
        });
        ajax.set(data.field);
        ajax.set("yyjl", $("#yyjl").find("option:selected").val());
        ajax.set("tiancai", $("#HandoverCompany").val());
        ajax.start();
    });
    form.on('select(tiancai)', function (data) {   //选择移交单位 赋值给input框
        var select_text = data.elem[data.elem.selectedIndex].text;
        $("#HandoverCompany").val(select_text);
        $("#tiancai").next().find("dl").css({ "display": "none" });
        form.render();
    });

});
